package components.dialogs
{
	import MLP.mlp_layer3d;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import classes.c_events;
	
	import components.editor.c_multiLayerElement;
	import components.propertyCtrls.c_numberCtrl;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class c_mlpDialog extends c_childListDialog {
		private var _current: DisplayObject;
		
		public function c_mlpDialog(parent: uif_component, a_owner: c_multiLayerElement, a_title:String='') {
			super(parent, a_owner.layer3D, a_title);
		}
		
		override protected function propertyList(): Array {
			var a: Array = super.propertyList();
			a.push({
				title	: 'Вращение по Y',
				type	: c_propertyDialog.PTYPE_NUMBER,
				value	: 0
			},{
				title	: 'Вращение по X',
				type	: c_propertyDialog.PTYPE_NUMBER,
				value	: 0
			},{
				title	: 'Грубина',
				type	: c_propertyDialog.PTYPE_NUMBER,
				value	: 0
			});
			return a;
		}
		
		override protected function checkElement(a_element: Object): Boolean {
			return true;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(c_events.CHANGECURRENTELEMENT, doChangeCurrent, cl.workspace);
		}
		
		private function doChangeCurrent(e: ObjectEvent): void {
			disable = e.object != owner.parent;
		}
		
		protected function getSelect(): DisplayObject {
			return (values[0].value > -1)?owner.getChildByName(valuesList[values[0].value].name):null;
		}
		
		protected function changeVisible(): void {
			for (var i: int=0; i<valuesList.length; i++) {
				var item: Object = valuesList[i];
				var child: DisplayObject = owner.getChildByName(item.name);
				child.visible = item.checked;
			}
		}
		
		protected function changeDepth(): void {
			_current.rotationY = values[1].value / 100 * 360;
			_current.rotationX = values[2].value / 100 * 360;
			_current.z = values[3].value * 4 - 200;
			(owner as mlp_layer3d).updateZOrder();
		}
		
		protected function setControlData(index: int, a_data: Object): void {
			(propertyCtrls[index].control as c_numberCtrl)._data = a_data
		}
		
		override protected function doChange(e: Event): void {
			var select: DisplayObject = getSelect();
			if (_current != select) {
				_current = select;
				setControlData(1, {
					value: _current.rotationY / 360 * 100
				});
				setControlData(2, {
					value: _current.rotationX / 360 * 100
				});
				setControlData(3, {
					value: (_current.z + 200) / 4
				})
			}
			if (_current) {
				changeVisible();
				changeDepth();
			}
		}
	}
}